package client;

import java.nio.FloatBuffer;
import java.util.Vector;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;

import shared.Tools;
import shared.Vec2f;
import vmo.Actor;

public class Actor_Render {

	Vector <Actor_Render_Unit> m_drawables; 
	int m_textureId;
	
	public Actor_Render(Vector <Actor> actors)
	{
		m_drawables=new Vector <Actor_Render_Unit>();
		for (int i=0;i<actors.size();i++)
		{
			Actor_Render_Unit aru=new Actor_Render_Unit(actors.get(i));
			m_drawables.add(aru);
		}
		m_textureId=Tools.loadPNGTexture("assets/art/spritesheet.png", GL13.GL_TEXTURE0);
	}
	
	public void Update(Vec2f position, int layer) //for figuring out which actors aren't visible
	{
		for (int i=0;i<m_drawables.size();i++)
		{
			if (m_drawables.get(i).m_actor.getLayer()==layer)
			{
				Vec2f p=m_drawables.get(i).m_actor.getPosition();
				if (p.x>position.x-32 && p.x<position.x+32)
				{
					if (p.y>position.y-32 && p.y<position.y+32)
					{
						m_drawables.get(i).m_actor.m_visible=true;
					}
					else
					{
						m_drawables.get(i).m_actor.m_visible=false;
					}
				}
				else
				{
					m_drawables.get(i).m_actor.m_visible=false;		
				}
			}
			else
			{
				m_drawables.get(i).m_actor.m_visible=false;			
			}
		}
		
	}
	
	public void AddActor(Actor actor)
	{
		m_drawables.add(new Actor_Render_Unit(actor));
	}
	
	public void Discard()
	{
		GL11.glDeleteTextures(m_textureId);		
	}
	
	public void Destroy_ARU(Actor actor)
	{
		for (int i=0;i<m_drawables.size();i++)
		{
			if (m_drawables.get(i).m_actor==actor)
			{
				m_drawables.remove(i);
				break;
			}
		}
		
	}
	
	public void Draw(FloatBuffer matrix44Buffer, int matrixloc,int colourloc)
	{
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, m_textureId);
		if (m_drawables.size()>0)
		{
			for (int i=0;i<m_drawables.size();i++)
			{
				Actor_Render_Unit A=m_drawables.get(i);
				if (A.m_actor.m_visible)
				{
					A.Draw(matrix44Buffer, matrixloc, colourloc);
				}
			}
		}
	}
	
	
}

